﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MM.Common.DBBase;

namespace MM.Common.Base
{
    internal class DbLogger : LogBase
    {
        // Methods
        public DbLogger(): this("", "", "")
        {
        }

        public DbLogger(string logname, string objectname, string eventname)
        {
            base._operId = SystemInfo.OperId;
            base._computerId = SystemInfo.ComputerID;
            base._systemCode = SystemInfo.SystemCode;
            base._logName = logname;
            base._objectName = objectname;
            base._eventName = eventname;
        }

        public override void WriteExp<T>(T exp)
        {
            string objectname = (base._objectName == "") ? " " : base._objectName;
            string eventname = (base._eventName == "") ? " " : base._eventName;
            this.WriteExp(objectname, eventname, exp.ToString());
        }

        public override void WriteExp(string objectname, string eventname, string info)
        {
            SystemInfo.DbData.TransactionRollback();
            string commandtext = "INSERT INTO MXHLY_SYS_ERROR ( EMP_ID, COMPUTER_CODE, SYSTEM_CODE,  ERROR_OBJECT,  ERROR_EVENT,  ERROR_TEXT)  VALUES(:emp_id, :node, :system_code, :eobject, :eevent, :info) ";
            DataParamCol parms = new DataParamCol();
            parms.AddBatch(new object[] { "emp_id", base._operId, "node", base._computerId, "system_code", base._systemCode, "eobject", objectname, "eevent", eventname, "info", info });
            try
            {
                SystemInfo.DbData.ExecuteSql(commandtext, parms);
                SystemInfo.DbData.TransactionCommit();
            }
            catch
            {
                SystemInfo.DbData.TransactionRollback();
            }
        }

        public override void WriteLog<T>(T info)
        {
            this.WriteLog(LogType.System, info.ToString());
        }

        public override void WriteLog(LogType type, string info)
        {
            string str = "";
            switch (type)
            {
                case LogType.System:
                    str = "S";
                    break;

                case LogType.Window:
                    str = "W";
                    break;

                case LogType.Button:
                    str = "B";
                    break;

                default:
                    str = "O";
                    break;
            }
            try
            {
                string commandtext = "INSERT INTO MXHLY_SYS_LOG (EMP_ID, TYPE ,COMPUTER_CODE ,INFO) VALUES (:userid, :type ,:node ,:info)";
                DataParamCol parms = new DataParamCol();
                parms.AddBatch(new object[] { "userid", base._operId, "type", str, "node", base._computerId, "info", info });
                SystemInfo.DbData.ExecuteSql(commandtext, parms);
            }
            catch (Exception exception)
            {
                throw new Exception("记录日志到数据库出错，" + exception.Message);
            }
        }
    }
}
